<?php

/**
 * Created by PhpStorm.
 * User: zxd
 * Date: 2019/3/3 0003
 * Time: 上午 11:33
 */


namespace p5\v2\db;

class SqlBean
{
    /**
     * @var $sql string
     */
    private $sql;
    private $params = [];

    static public function makeInsertIgnoreInstance($table, $data)
    {
        return static::buildInsertInstance($table, $data, "insert ignore");
    }

    static public function makeInsertInstance($table, $data)
    {
        return static::buildInsertInstance($table, $data, "insert");
    }

    static public function makeReplaceInstance($table, $data)
    {
        return static::buildInsertInstance($table, $data, "replace");
    }

    static public function makeInsertsIgnoreInstance($table, $data)
    {
        return static::buildInsertsInstance($table, $data, "insert ignore");
    }

    static public function makeInsertsInstance($table, $data)
    {
        return static::buildInsertsInstance($table, $data, "insert");
    }

    static public function makeReplacesInstance($table, $data)
    {
        return static::buildInsertsInstance($table, $data, "replace");
    }

    /**
     * @param $table string
     * @param $data array
     * @param string $insertFlag
     * @return SqlBean
     */
    static private function buildInsertInstance($table, $data, $insertFlag = "insert")
    {
        $columns = array_keys($data);
        $count = count($columns);
        $temp = static::toQString($count);
        $string = implode(",", $columns);
        $params = array_values($data);
        $bean = new SqlBean();
        $sql = "{$insertFlag} into {$table} ($string) values ({$temp})";
        $bean->setSql($sql);
        $bean->setParams($params);
        return $bean;
    }

    static private function buildInsertsInstance($table, $data, $insertFlag = "insert")
    {
        if(empty($data)){
            return new SqlBean() ;
        }
        $temp = static::toArray($data[0]);
        $columns = array_keys($temp);
        $string = implode(",", $columns);
        $valueParts = [];
        for ($i = 0; $i < count($data); $i++) {
            $body = static::buildInsertValuePart($data[$i]);
            array_push($valueParts, $body);
        }
        $valueString = implode(",", $valueParts);
        $bean = new SqlBean();
        $sql = "{$insertFlag} into {$table} ($string) values {$valueString}";
        $bean->setSql($sql);
        $bean->setParams([]);
        return $bean;
    }

    static private function buildInsertValuePart($line)
    {
        $line = static::toArray($line);
        $columns = array_keys($line);
        $values = [];
        foreach ($columns as $column) {
            $value = $line[$column];
            $value = \p5\objects\SqlMaker::sql_safe_replace($value);
            array_push($values, "'{$value}'");
        }
        return "(" . implode(",", $values) . ")";
    }

    static private function toArray($line){
        if (!is_array($line) && is_object($line)) {
            $line = json_decode(json_encode($line), true);
        }
        return $line ;
    }
    static private function toQString($count)
    {
        $temp = str_repeat(",?", $count);
        return substr($temp, 1);
    }

    /**
     * @return string
     */
    public function getSql()
    {
        return $this->sql;
    }

    /**
     * @param string $sql
     */
    public function setSql($sql)
    {
        $this->sql = $sql;
    }

    /**
     * @return array
     */
    public function getParams()
    {
        return $this->params;
    }

    /**
     * @param array $params
     */
    public function setParams($params)
    {
        $this->params = $params;
    }


}